﻿using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;

namespace JNPF.Utils
{
    /// <summary>
    /// RedisHelper
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// </summary>
    public class RedisHelper : ICache
    {
        private IDatabase db = null;
        public RedisHelper()
        {
            try
            {
                db = RedisConnection.Connection.GetDatabase();
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        public List<string> GetAllKeys()
        {

            var connection = RedisConnection.Connection;
            var list = new List<string>();
            foreach (var ep in connection.GetEndPoints())
            {
                var server = connection.GetServer(ep);
                foreach (var item in server.Keys(db.Database))
                {
                    list.Add(item);
                }
            }
            return list;
        }
        public DateTime GetLiveTime(string key)
        {
            try
            {
                var timeSpan = (TimeSpan)db.KeyTimeToLive(key);
                return DateTime.Now.Add(timeSpan);
            }
            catch (Exception)
            {
                return DateTime.Now;
            }
        }
        public string Get(string key)
        {
            var str = db.StringGet(key).ToString();
            if (str != null)
                return str.Trim('"');
            else
                return null;
        }
        public T Get<T>(string key) where T : class
        {
            var obj = db.StringGet(key).ToString();
            if (obj != null)
                return JsonConvert.DeserializeObject<T>(obj);
            else
                return default(T);
        }
        public bool Exists(string key)
        {
            return db.KeyExists(key);
        }
        public List<string> GetHashValues(string key)
        {
            List<string> result = new List<string>();
            HashEntry[] arr = db.HashGetAll(key);
            foreach (var item in arr)
            {
                result.Add(item.Value);
            }
            return result;
        }
        public List<string> GetHashKeys(string key)
        {
            List<string> result = new List<string>();
            RedisValue[] arr = db.HashKeys(key);
            foreach (var item in arr)
            {
                result.Add(item);
            }
            return result;
        }
        public void Insert<T>(string key, T obj) where T : class
        {
            db.StringSet(key, JsonConvert.SerializeObject(obj), TimeSpan.FromMinutes(10));
        }
        public void Insert<T>(string key, T obj, TimeSpan? expiry) where T : class
        {
            db.StringSet(key, JsonConvert.SerializeObject(obj), expiry);
        }
        public void InsertHash(string key, string hashId, string value)
        {
            db.HashSet(key, hashId, value);
        }
        public void Remove(string key)
        {
            if (db.KeyExists(key))
                db.KeyDelete(key);
        }

    }
}
